﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//闹钟类
public class Alarm
{
    public TimeSpan AlarmTime { get; set; }     //响铃持续时间
    public RepeatMode Mode { get; set; }      //重复模式
    public DayOfWeek? WeeklyDay { get; set; }      //每周一次模式下周x重复
    public DateTime? OnceDateTime { get; set; }    //响铃时间
    public string Sound { get; set; } //铃声
    public bool IsActive { get; set; } = true;        //是否被激活（可以设置为开启和关闭两个状态）

    public bool ShouldTrigger(DateTime currentTime)          //判断闹钟是否响铃
    {
        if (!IsActive) return false;        //判断闹钟是否被开启

        bool timeMatches = currentTime.TimeOfDay.Hours == AlarmTime.Hours &&
                          currentTime.TimeOfDay.Minutes == AlarmTime.Minutes &&
                          currentTime.Second == 0;

        if (!timeMatches) return false;         //判断时间是否匹配

        return Mode switch
        {
            RepeatMode.Once => currentTime >= OnceDateTime && currentTime < OnceDateTime?.AddMinutes(1),    //不重复
            RepeatMode.Daily => true,      //每天重复
            RepeatMode.Workdays => currentTime.DayOfWeek is >= DayOfWeek.Monday and <= DayOfWeek.Friday,    //工作日重复
            RepeatMode.Weekly => currentTime.DayOfWeek == WeeklyDay,    //每周x重复
            _ => false
        };
    }
}
